﻿
function extraInit()
{
    initTree(false, false, false);
}


function selectMonth()
{
    var month = parseInt($("input[name=tbxMonth]").val());
    var year = parseInt($("input[name=tbxYear]").val());
    if (isNaN(month) || isNaN(year))
    {
        var date = new Date();
        month = date.getMonth();
        year = date.getFullYear();
    }
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function previousMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month--;
    if (month < 1)
    {
        year--;
        month = 12;
    }
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function nextMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month++;
    if (month > 12)
    {
        year++;
        month = 1;
    }
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function previousYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year--;
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function nextYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year++;
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function selectTreeNode(jobTitleId)
{
    //$("input[name=jobTitleId]").val(jobTitleId);
    //$("#btnViewMonth").click();
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function selectPage(index)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, index);
}

function viewWorktimeSheet(jobTitleId, year, month, pageIndex)
{
    $("input[name=tbxMonth]").val(month);
    $("input[name=tbxYear]").val(year);
    $("#hdfMonth").val(month);
    $("#hdfYear").val(year);
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Salary/AjaxBasicSalary.aspx",
	    data : { 
		    "action" : "get_monthly_basic_salary", 
		    "jobTitleId" : jobTitleId,
		    "year" : year,
		    "month" : month,
		    "pageIndex" : pageIndex
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    var table = $("#worktimeTable");
			    var rows = table.find(".div-row");
			    for (var i=rows.length-1; i>0; i--)
			    {
			        rows.eq(i).remove();
			    }
			    for (var i=0; i<r.list.length; i++)
			    {
			        var htmlRow = "<div class='div-row'>";
			        htmlRow += "<div class='div-cell c0'>"+r.list[i].order+"</div>";
			        htmlRow += "<div class='div-cell c4'>";
			        htmlRow += "<input type='hidden' name='hdfAgentId' value='"+r.list[i].agentId+"'/>";
			        htmlRow += "<span class='edit' onclick='editBasicSalary(this);'></span>";
			        htmlRow += r.list[i].fullName;
			        htmlRow += "</div>";
			        htmlRow += "<div class='div-cell c6'><span class='detail2'>"+r.list[i].salaryLevel+"</span></div>";
			        htmlRow += "<div class='div-cell c5'><span class='detail'>"+r.list[i].salaryLevelSalaryCalc+"</span></div>";
			        htmlRow += "<div class='div-cell c5'><span class='detail2'>"+r.list[i].senority+"</span></div>";
			        htmlRow += "<div class='div-cell c2'><span class='detail'>"+r.list[i].senoritySalaryCalc+"</span></div>";
			        htmlRow += "<div class='div-cell c2'><span class='detail'>"+r.list[i].totalBasicSalary+"</span></div>";
                    table.append(htmlRow);
			    }
			    
			    var divPaging = $("#divPaging");
			    var paging = "";
			    for (var i=1; i<=r.totalPage; i++)
			    {
			        if (r.pageIndex == i)
			        {
			            paging += "<span>"+i+"</span>";
			        }
			        else
			        {
			            paging += "<a href='javascript:void(0);' onclick='selectPage("+i+");'>"+i+"</a>";
			        }
			    }
			    divPaging.html(paging);
			}
	    }
    })
    .always(function() {
	    unmask(area);
	});
}

function editBasicSalary(button)
{
    var cells = $(button).closest(".div-row").find(".div-cell");
    var cellSalary = cells.eq(2);
    var cellSenority = cells.eq(4);
    if (cellSalary.find("span").length > 0)
    {
        //change to edit mode
        var salaryLevel = cellSalary.find("span").text();
        var input = "<input type='text' name='salaryLevel' value='"+salaryLevel+"' onkeydown='enter(this, event);'/>";
        cellSalary.html(input);
        var senority = cellSenority.find("span").text();
        input = "<input type='text' name='salaryLevel' value='"+senority+"' onkeydown='enter(this, event);'/>";
        cellSenority.html(input);
        
        cellSalary.find("input").focus();
    }
    else
    {
        //update data
        updateBasicSalary(cells);
    }
}

function enter(input, evt)
{
    if (evt.keyCode != 13)
    {
        return false;
    }
    var cells = $(input).closest(".div-row").find(".div-cell");
    updateBasicSalary(cells);
    
    return true;
}

function updateBasicSalary(cells)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    
    var cellSalary = cells.eq(2);
    var cellSalaryLevelCalc = cells.eq(3);
    var cellSenority = cells.eq(4);
    var cellSenoritySalaryCalc = cells.eq(5);
    var cellTotalBasicSalary = cells.eq(6);
    var salaryLevel = cellSalary.find("input").val();
    var senority = cellSenority.find("input").val();
    var agentId = cells.eq(1).find("input[name=hdfAgentId]").val();
   
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Salary/AjaxBasicSalary.aspx",
	    data : { 
		    "action" : "update_monthly_basic_salary", 
		    "year" : year,
		    "month" : month,
		    "agentId" : agentId,
		    "salaryLevel" : salaryLevel,
		    "senority" : senority
	    }, 
	    success : function (r) {
	        if (r.status)
			{
                var span = "<span class='detail2'>"+r.salaryLevel+"</span>";
                cellSalary.html(span);
                span = "<span class='detail'>"+r.salaryLevelSalaryCalc+"</span>";
                cellSalaryLevelCalc.html(span);
                span = "<span class='detail2'>"+r.senority+"</span>";
                cellSenority.html(span);
                span = "<span class='detail'>"+r.senoritySalaryCalc+"</span>";
                cellSenoritySalaryCalc.html(span);
                span = "<span class='detail'>"+r.totalBasicSalary+"</span>";
                cellTotalBasicSalary.html(span);
            }
            else
            {
                if (r.error != "")
                {
                    alert(r.error);
                }
            }
        }
    })
    .always(function() {
	    unmask(area);
	});
}

function exportExcel()
{
    var li = $("a.jstree-clicked").closest("li");
    if (li.length  == 0)
    {
        return;
    }
    $("#hdfJobTitleId").val(li.attr("id"));
}



